package com.gray.controller;


import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gray.common.UploadImage;
import com.gray.pojo.ArticleSort;
import com.gray.pojo.User;
import com.gray.service.impl.ArticleServiceImpl;

@Controller
@RequestMapping("/backStage")
public class BackStageController {
	
	@Autowired
	private ArticleServiceImpl articleImpl;
	
	@RequestMapping("top")
	public String top(){
		return "system/top";
	}
	@RequestMapping("bottom")
	public String bottom(){
		return "system/bottom";
	}
	@RequestMapping("article")
	public String article(){
		return "system/article";
	}
	@RequestMapping("diary")
	public String diary(){
		return "system/updateuser";
	}
	@RequestMapping("myself")
	public String myself(HttpServletRequest req){
//		System.out.println(req.getRemoteAddr());
//		System.out.println(req.getRemoteUser());
		return "system/myself";
	}
	@RequestMapping("stayMessage")
	public String stayMessage(){
		return "system/staymessage";
	}
	
	@RequestMapping("pic")
	public String pic(@RequestParam MultipartFile img){
		
		System.out.println(img.getOriginalFilename());
		try {
			img.transferTo(new File("C:\\test\\",img.getOriginalFilename()));
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	/*
	 * 2017年10月5日 15:01:32
	 */
	
	//跳转添加文章页面
	@RequestMapping("addarticle")
	public String Article(){
		
		return "system/article/addarticle";
	}
	
	@RequestMapping(value="deletearticle")
	public String deleteArticle(){
		return "system/article/deletearticle";
	}
	
	/** 添加文章
	 * 
	 * @param articleName 标题
	 * @param articleSortID 分组id
	 * @param content  内容
	 * @param typeId 所属栏目
	 * @param articleType 是否公开
	 * @param req
	 * @param identifyingCode 用户输入的验证码
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addArticle",method=RequestMethod.POST)
	public int addArticle( @RequestParam(required=false) String articleName,
			@RequestParam(required=false) Integer articleSortID,
			@RequestParam(required=false) String articleContent,
			@RequestParam(required=false) String typeId,
			@RequestParam(required=false) Integer articleType,
			@RequestParam(required=true) String identifyingCode,
			@RequestParam(required=false) MultipartFile file,
			HttpServletRequest request,
			HttpServletResponse response){
		User user	= (User) request.getSession().getAttribute("user");
		Integer userId = user.getUserId();
		com.gray.pojo.Article article = new com.gray.pojo.Article();
		String articleIp =request.getRemoteAddr(); //获取ip
		
		//正确的验证码
		String kaptchaExpected = (String) request.getSession().getAttribute(  
                com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY);  
		System.out.println("输入的验证码为:"+identifyingCode+"正确的验证码为:"+kaptchaExpected);
		if(!identifyingCode.equals(kaptchaExpected)){
			return -1;
		}
		article.setArticleIp(articleIp);
		article.setArticleContent(articleContent);
		article.setArticleName(articleName);
		article.setSortArticleId(articleSortID);
		article.setTypeId(Byte.parseByte(typeId));
		article.setUserId(userId);
		article.setArticleType(articleType);
		System.out.println(identifyingCode);
		System.out.println(kaptchaExpected);
		UploadImage uploadImage = new UploadImage();
		try {
			uploadImage.setInputStream(file.getInputStream());
			uploadImage.setOriginalFilename(file.getOriginalFilename());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(uploadImage.getOriginalFilename());
		return articleImpl.addArticle(article,uploadImage);
	}
	
	/**
	 * 跳转文章分组操作页面
	 * @return
	 */
	@RequestMapping(value="articlesortoperation",method=RequestMethod.GET)
	public String articleSortOperation(){
		
		return "system/article/articlesortoperation";
	}
	
	/**
	 * 获取文章分组类别
	 * @param req
	 * @return
	 */
	
	
	@RequestMapping(value="getarticlesort")
	@ResponseBody
	public List<ArticleSort> getArticleSort(HttpServletRequest req){
		  User user =(User) req.getSession().getAttribute("user");
		List list = new ArrayList() ;
		
		
		if( user!=null){
			list = articleImpl.getAllArticleSort(user);
		}
		
		return list;
	}
	
	/**
	 * 删除文章分组 （单个）
	 * @param req
	 * @param sortArticleId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delarticlesort")
	public int delArticleSort(HttpServletRequest req,@RequestParam int sortArticleId ){
		User user =(User) req.getSession().getAttribute("user");
		if(user==null)
			return 0;
		ArticleSort articleSort = new ArticleSort();
		articleSort.setUserId(user.getUserId());
		articleSort.setSortArticleId(sortArticleId);
		return articleImpl.delArticleSort(articleSort);
	}
	
	/**
	 * 添加文章分组类别
	 * @param req
	 * @param sort_article_name
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="addarticlesort")
	public int addArticleSort(HttpServletRequest req,@RequestParam String sort_article_name ){
		User user =(User) req.getSession().getAttribute("user");
		if(user==null)
			return 0;
		ArticleSort articleSort = new ArticleSort();
		System.out.println(sort_article_name);
		articleSort.setUserId(user.getUserId());
		articleSort.setSort_article_name(sort_article_name);
		return articleImpl.insertArticleSort(articleSort);
	}
	
	/**
	 * 跳转到更新密码界面
	 * @param request
	 * @return
	 */
	@RequestMapping(value="turnUpdatepassword")
	public String turnUpdatePassWord(HttpServletRequest request){
		return "system/updatepassword";
	}
}
